﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Messaging;

namespace Mangox.Common
{
    /// <summary>
    /// 文档：https://www.cnblogs.com/sxxsw/p/5061259.html
    /// 微软消息队列 MSMQ
    /// 
    /// RabbitMQ消息队列
    /// https://www.cnblogs.com/kangkey/p/6793273.html
    /// </summary>
    public class MQHelper
    {
        /// <summary>
        /// 创建一个消息队列
        /// </summary>
        /// <param name="name">消息队列的名称</param>
        /// <returns>是否创建成功</returns>
        public static bool CreateNewMQ(string name)
        {
            if (!MessageQueue.Exists(".\\private$\\" + name))
            {

                MessageQueue mq = MessageQueue.Create(".\\private$\\" + name);
                mq.Label = "private$\\" + name;
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除一个消息队列
        /// </summary>
        /// <param name="name">消息队列的名称</param>
        /// <returns>是否创建成功</returns>
        public static bool DeleteNewMQ(string name)
        {
            if (!MessageQueue.Exists(".\\private$\\" + name))
            {
                MessageQueue.Delete(".\\private$\\" + name);
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 发送消息到指定消息队列
        /// </summary>
        /// <param name="mq_name">消息队列名称</param>
        /// <param name="msg_lable">消息头</param>
        /// <param name="msg_body">消息体</param>
        public static void SendMessage(string mq_name, string msg_lable, string msg_body)
        {
            MessageQueue mq = new MessageQueue(@".\private$\" + mq_name);
            Message message = new Message();
            message.Label = msg_lable;
            message.Body = msg_body;
            mq.Send(message);
        }
        /// <summary>
        /// 从指定消息队列获取第一条消息
        /// </summary>
        /// <param name="mq_name">消息队列名称</param>
        /// <returns>Message</returns>
        public static Message ReceiveMessage(string mq_name)
        {
            MessageQueue mq = new MessageQueue(@".\private$\" + mq_name);
            if (mq.GetAllMessages().Length > 0)
            {
                Message message = mq.Receive();
                if (message != null)
                {
                    message.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(string) });
                }
                return message;
            }
            else
            {
                return null;
            }
        }
    }
}


/* wpf调用是咧
using System;
using System.Timers;
using System.Windows;

namespace MQ
{
    public partial class MainWindow : Window
    {
        private static readonly string mq_name = "textMQ";
        private Timer timer;

        public MainWindow()
        {
            InitializeComponent();
            MQHelper.CreateNewMQ(mq_name);
            timer = new Timer();
            timer.Interval = 1000;
            timer.Elapsed += new System.Timers.ElapsedEventHandler(flash);
            timer.Start();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string mq_content = inputText.Text;
            //发送一条消息到消息队列
            MQHelper.SendMessage(mq_name, "test", mq_content);
        }

        //循环获取消息队列中的消息，并展现到TextBox中
        private void flash(object sender, ElapsedEventArgs e)
        {
            var message = MQHelper.ReceiveMessage(mq_name);
            if (message != null)
            {
                Action hide = delegate()
                {
                    outputText.Text += message.Id + " " + message.Label + " " + message.Body + "\n";
                };
                this.Dispatcher.BeginInvoke(hide);
            }
        }
    }
}
*/
